﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using frontend_wcf.WCFService;
using System.IO;

namespace frontend_wcf
{
    public partial class FrmMain : Form
    {
        ServiceClient service;
        UserType userType = UserType.guest;

        private string username;
        public string Username
        {
            set { username = value; }
        }

        private string password;
        public string Password
        {
            set { password = value; }
        }
        
        public FrmMain()
        {
            service = new ServiceClient("WSHttpBinding_IService");
            //service.ClientCredentials.Windows.ClientCredential.UserName = "Kristoffer";
            //service.ClientCredentials.Windows.ClientCredential.Password = "";
            //service.ClientCredentials.Windows.ClientCredential.Domain = "LIZZY";

            //service.ClientCredentials.UserName.Password = "";
            //service.ClientCredentials.UserName.UserName = "LIZZY\\Kristoffer";
            //service = new ServiceClient("HttpStreaming");
            InitializeComponent();
        }

        public void reloadFiles()
        {
            DummyFile[] files = service.viewFiles();
            int y = 0;
            file_panel.Controls.Clear();
            foreach (DummyFile file in files)
            {
                FileListItem listItem = new FileListItem(service, this, file, userType);

                listItem.Top = y;
                listItem.Left = 2;
                file_panel.Controls.Add(listItem);

                y = listItem.Bottom + 5;
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            reloadFiles();
            fixButtonRights();
        }

        private void login(string username, string password)
        {
            userType = service.login(username, password);
            //userType = UserType.admin;
            fixButtonRights();
            reloadFiles();
        }

        private void logout()
        {
            userType = UserType.guest;
            fixButtonRights();
            reloadFiles();
        }

        private void fixButtonRights()
        {
            if (userType == UserType.admin)
            {
                group_Admin.Show();
                group_Tools.Show();
            }
            else if (userType == UserType.normal)
            {
                group_Tools.Show();
                group_Admin.Hide();
            }
            else
            {
                group_Tools.Hide();
                group_Admin.Hide();
            }
        }

        private void addNewFile(string path, string name)
        {
            byte[] rawData;
            FileStream fs;
            fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            long FileSize = fs.Length;

            rawData = new byte[FileSize];
            fs.Read(rawData, 0, Convert.ToInt32(FileSize));
            fs.Close();

            service.addFile(name, rawData);

            reloadFiles();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void loginMenuItem_Click(object sender, EventArgs e)
        {
            
            if (userType == UserType.guest)
            {
                if ((new FrmLogin(this)).ShowDialog() == DialogResult.OK)
                {
                    login(username, password);
                    if (userType == UserType.guest)
                    {
                        MessageBox.Show("Login failed");
                    }
                }
            }
            else
            {
                logout();
            }

            if (userType == UserType.guest)
            {
                loginMenuItem.Text = "Login";
            }
            else
            {
                loginMenuItem.Text = "Logout";
            }
        }

        private void btn_AddFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filepath = openFileDialog.FileName;
                string filename = openFileDialog.SafeFileName;
                addNewFile(filepath, filename);
            }
        }

        private void file_panel_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void file_panel_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                addNewFile(file, fileInfo.Name);
            }
        }

        private void btn_AddUser_Click(object sender, EventArgs e)
        {
            (new FrmAddUser(service)).ShowDialog();
        }
    }
}
